<template>
	<view  style="padding-bottom: 40rpx;">
		
			<view class="release_content">
				
				    <image v-if="imgList.length" :src="imgList[current]" mode="aspectFit"></image>
			
						
				<view class="acea-row row-between actionBottom">
				    <view class="action acea-row row-center" @click="reset">
				        <image src="/static/images/reset.png" mode="widthFix"></image>
				    </view>
                    <view class="action acea-row row-center" @click="del()">
                        <image src="/static/images/del.png" mode="widthFix"></image>
                    </view>
                   <view class="action acea-row row-center" @click="savePosterPath()">
                        <image src="/static/images/down.png" mode="widthFix"></image>
                    </view>
                   <!-- <view class="action acea-row row-center" @click="add">
                        <image src="/static/images/like.png" mode="widthFix"></image>
                    </view> -->
                    <view class="publish" @click="publish">
                        发布
                    </view>
				</view>
			</view>
            <view class="bottom">
               <view class="resulttitle">
                   设计结果
               </view>
               <view class="imgbox ">
                   <view  v-for="(i,n) in imgList" :key="n" :class="current==n?'active imgItem':'imgItem'" @click="changeAct(n)">
                       <image  :src="i" mode=""></image>
                   </view>
                   
               </view>
            </view>
                <view class="resulttitle " style="margin: 0 48rpx;">
                    店铺推荐
                </view>
                <view class="bottom merchatItem acea-row row-between" v-for="(i,n) in merchantList " :key="n" @click="toStore(i)">
                        <image  :src="i.mer_avatar" mode=""></image>
                        <view class="">
                             <text>{{i.mer_name}}</text>
                        </view>
                </view>
	</view>	
</template>

<script>
	
	import { mapGetters } from "vuex";
	import {TOKENNAME, HTTP_REQUEST_URL} from '@/config/app.js';
	import { toLogin } from '@/libs/login.js';
	import {AiStatus,AiWaitingEnd,lanGangMer} from "@/api/ai";
	import { getUserInfo } from '@/api/user.js';
	import { setStorage, getStorage } from '../../../libs/uniApi.js';
	import easyLoadimage from '@/components/easy-loadimage/easy-loadimage.vue';
	import { configMap } from '@/utils';
    import { storeMerchantList, getGeocoder } from '@/api/store.js';
	import store from '@/store';
	export default {
		components: {
			
		},
		data() {
			return {
				desc:'',
                paramsStr:'',
                imgList:[],
                current:0,
                merchantList:[],
                cate:"",
                cate1:"",
			};
		},
		computed: {
			...mapGetters(['isLogin', ]),
			
		
		},
		watch:{},
		mounted() {
		
		},
		onLoad(opt) {
			if (!this.isLogin) {
				
			}else{
                if(opt.cate) this.cate = opt.cate
                if(opt.cate1) this.cate1 = opt.cate1
				if(opt.paramsStr){
                    
                    this.paramsStr=opt.paramsStr
                    
                    // this.getAiTask()
                }
                this.getAiTask()
			}
            this.storeMerchantList()
			uni.removeStorageSync('paramStr')
		},
		methods: {	
            reset(){
                uni.navigateBack()
            },
            async storeMerchantList(){
                    let rqData = {
                    	from:'recommand',
                    	page: 1,
                    	limit: 10,
                    	
                    }
            const {data} = await lanGangMer(rqData)   
             console.log(data)
            this.merchantList = data.data
            },
            async getAiTask(){
                let data=  await  AiStatus({method:'POST', api:'progress',paramsStr:`req_id=${this.paramsStr}`})
                console.log(data)
                this.imgList=data.data.data
            },
            changeAct(n){
                this.current=n
            },
            del(){
                let vm =this
                uni.showModal({
                    content:'是否删除该创作',
                    cancelText:'取消',
                    confirmText:'确认',
                    success:function(res){
                        if(res.confirm){
                            vm.imgList.splice(vm.current,1)
                        }
                    }
                })
            },
            publish(){
                    uni.navigateTo({
                        url:'/pages/plantGrass/publish/index?paramsStr='+this.paramsStr+"&cate="+this.cate+"&cate1="+this.cate1+'&imgList='+JSON.stringify(this.imgList)
                    })
            },
            fileStoreImage(url) {
            	let that = this;
            	// #ifdef MP  || APP-PLUS
            	let ishttps = url.split('//')[0] == 'https:'
            	if (!ishttps) {
            		url = 'https://'+url.split('//')[1]
            	}
            	// #endif
            	return new Promise((resolve, reject) => {
            		uni.downloadFile({
            			url: url,
            			success: function(res) {
            				resolve(res.tempFilePath);
            			},
            			fail: function() {
            				return that.$util.Tips({
            					title: ''
            				});
            			}
            		});
            	})
            },
            async savePosterPath() {
                uni.showLoading({
                    title:'保存中'
                })
                let img = await this.fileStoreImage(this.imgList[this.current])
            	let that = this;
            	uni.getSetting({
            		success(res) {
            			if (!res.authSetting['scope.writePhotosAlbum']) {
            				uni.authorize({
            					scope: 'scope.writePhotosAlbum',
            					success() {
                                    uni.hideLoading()
            						uni.saveImageToPhotosAlbum({
            							filePath: img,
            							success: function(res) {
            								
            								that.$util.Tips({
            									title: '保存成功',
            									icon: 'success'
            								});
            							},
            							fail: function(res) {
            								that.$util.Tips({
            									title: '保存失败'
            								});
            							}
            						})
            					}
            				})
            			} else {
            				uni.saveImageToPhotosAlbum({
            					    filePath: img,
            					success: function(res) {
            						uni.hideLoading()
                                    
            						that.$util.Tips({
            							title: '保存成功',
            							icon: 'success'
            						});
            					},
            					fail: function(res) {
                                    uni.hideLoading()
            						that.$util.Tips({
            							title: '保存失败'
            						});
            					},
            				})
            			}
            		}
            	})
            },
            toStore(item){
                uni.navigateToMiniProgram({
                    appId:'wx19ad85594f6887ce',
                    envVersion:'release',
                    path: 'pages/store/home/index?mer_id='+item.mer_id,
                    
                })
                // uni.navigateTo({
                //     url:'/pages/store/home/index?merData='+JSON.stringify(item)
                // })
            }
        }    
	}
</script>

<style lang="scss" scoped>
    .release_content{
        background-color: #1C1C1E;
        border-radius: 12rpx;
        margin:20rpx 48rpx;
        height: 850rpx;
        padding: 40rpx;
        text-align: center;
        position: relative;
        image{
            width: 560rpx;
            
            height: 600rpx;
            
        }
        
    }
  
    .bottom{
        margin: 48rpx ;
        background-color: #1C1C1E;
        border-radius: 12rpx;
        padding: 24rpx;
        
        image{
           width: 156rpx;
           height: 156rpx;
        }
            
        .imgItem{
            width: 160rpx;
            height: 160rpx;
            border-radius: 6rpx;
            margin-right: 20rpx;
            display: inline-block;
        }
        .active{
                border: 1px solid #A187F9;
        }
     
    }
    .resulttitle{
        margin-bottom: 20rpx;
        color: #9a9a9a;
    }
    .active{
        
    }
    .action{
        width: 94rpx;
        height: 60rpx;
        border-radius: 8rpx;
        align-items: center;
        background-color: rgba(187,187,187,1);
        image{
            width: 36rpx;
        }
        
    }
    .imgbox{
            overflow: scroll;
            white-space: nowrap;
            
            width: 100%;
            display: block;
    }
    .actionBottom{
        position: absolute;
        width: 100%;
    bottom: 50rpx;
    left: 0;
    padding:0 40rpx;
    }
    .publish{
        width: 150rpx;
        height: 60rpx;
        box-sizing: border-box;
        border-radius: 16rpx;
        background-color: rgba(230,34,80,1);
        color: rgba(255,255,255,1);
        font-size: 28rpx;
        line-height: 60rpx;
        text-align: center;
        font-family: Microsoft Yahei;
        
    }
    .merchatItem{
        image{
            width: 176rpx;
            height: 162rpx;
            border-radius: 10rpx;
            margin-right: 20rpx;
        }
        view{
            flex: 1;
            color: #999;
        }
    }
</style>
